<?php
/**
 * Description of ResourceParameterController
 *
 * @author cybill
 */
require_once 'BaseController.php';

class Admin_ResourceParameterController extends Admin_BaseController {
    
    private $_session;
    public static $_Param_NS = 'Web_Resource_Param';
    
    public function init() {
        parent::init();
        
        $this->_session = new Zend_Session_Namespace(self::$_Param_NS);
    }
    
    public function indexAction()
    {
        $request = $this->getRequest();
        
        if (isset($this->_session->resource_id)) {
            $id = $this->_session->resource_id;
        } else {
            $id = $request->getParam('resid');
            $this->_session->resource_id = $id;
        }
        
        $this->_session->resource_id = $id;
        $this->view->resource_id = $this->_session->resource_id;
        
        if ($this->httpRequest()->isXmlHttpRequest()) {
            $q = $request->getParam('q');
            $page = $request->getParam('page');
            $id = $this->_session->resource_id;
            
            if (empty($id)) {
                $this->_helper->json(array(
                    'errorplus' => 'You have to choose at least 1 product / resource, with clicking \'Edit Parameter\'',
                    'url' => $this->view->baseUrl('admin/resource')
                ));
            }
            
            $where = array('resID = ?' => $id);
            $stat = $this->_helper->ListPage('Admin_Model_ResourceParameterService', $page, $where);
            
            $options = array(
                'limit'=>
                    array(
                        $stat['perpage'] => $stat['offset'])                
            );

            $service = AppRegistry::loadService('Admin_Model_ResourceParameterService');
            $data = $service->findAll($where, $options);
            
            if ($data) {
                $this->_helper->json(array(
                    'data' => $data, 
                    'pview'=> $stat['pview'], 
                    'offset'=> $stat['offset'],
                    'fieldname' => 'mandatory'
                    ));
                
            } else {
                $this->_helper->json(array('error'=>'No Data Found.'));
            }
        }
    }        
    
    public function editAction() {
        $resid = $this->_session->resource_id;        
        $id = $this->getParam('id');
        
        $service = AppRegistry::loadService('Admin_Model_ResourceParameterService');
        $data = $service->getRow($id);

        $md = array(
            array('mandatory'=>'0', 'name'=>'Yes'),
            array('mandatory'=>'1', 'name'=>'No')
        );
        
        $mandatory = $this->_helper->CatList($md, $data['mandatory']);
        
        $this->view->data = $data;
        $this->view->mandatory = $mandatory;
        $this->view->resource_id = $resid;
    }
    
    public function addAction() {
        $id = $this->_session->resource_id;
        $md = array(
            array('mandatory'=>'0', 'name'=>'Yes'), 
            array('mandatory'=>'1', 'name'=>'No')
        );
        
        $mandatory = $this->_helper->CatList($md);
        
        $this->view->mandatory = $mandatory;
        $this->view->resource_id = $id;
    }
    
    public function saveAction() {
        $id = $this->getParam('id');
        $resid = $this->getParam('resid');
        $name = $this->getParam('name');        
        $example_value = $this->getParam('value');
        $description = $this->getParam('desc');
        $mandatory = $this->getParam('mandatory');
        $example_request = $this->getParam('example');
        
        $data = array(
            'resID' => $resid,
            'name' => $name, 
            'value' => $example_value,
            'description' => $description,
            'mandatory' => $mandatory,
            'example' => $example_request
        );
        
        foreach($this->baseUserLogin($id) as $key => $value) $data[ $key ] = $value;
        
        try {
            $service = AppRegistry::loadService('Admin_Model_ResourceParameterService');
            $save = (!isset($id) ? $service->doSave($data) : $service->doUpdate($data, $id));

            $this->_helper->json(array('status'=>($save ? 'Save Success.' : 'Nothing changed.')));            
        } catch (Exception $exc) {
            $this->_helper->json(array('status'=>$exc->getMessage()));            
        }        
        
        $this->_helper->viewRenderer->setNoRender();
    }

    public function deleteAction() {
        try {
            $id = $this->getParam('id');
            $service = AppRegistry::loadService('Admin_Model_ResourceParameterService');
            
            $delete = $service->doDelete($id);
            $this->_helper->json(array('status'=>($delete ? 'Delete success.' : 'Nothing deleted.')));
            
        } catch (Exception $exc) {
            $this->_helper->json(array('status'=>$exc->getTraceAsString()));
        }
    }            
}

?>
